package controller

import (
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"week3zk-project/api/pkg"
)

func OssUpload(c *gin.Context) {
	file, _ := c.FormFile("file")
	log.Println(file.Filename)
	dst := "./img" + file.Filename
	// 上传文件至指定的完整文件路径
	err := c.SaveUploadedFile(file, dst)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "图片上传失败",
			"err":  err.Error(),
		})
		return
	}
	pkg.OSSCloudUpload(file.Filename, dst)
	if file.Size > 1024*1024*20 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "图片不能过大",
			"err":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "图片上传成功",
	})
}
